package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
//@Transactional mongodb事务必须复制集方式
public class RegionServiceImpl implements IRegionService{

    @Autowired
    private RegionRepository repository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(Region region) {
        region.setId(null);//不让mongod误会：
        repository.save(region);
    }

    @Override
    public void update(Region region) {
        repository.save(region);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Region get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Region> list() {
        return repository.findAll();
    }

    @Override
    public Page<Region> query(RegionQuery qo) {
        //Page  核心：currentpage pagesize total list totalPage  next pre

        //MQL 语句拼接类  查询封装类
        Query query=new Query();
        //拼接条件
        long total=template.count(query, Region.class);
        if (total == 0) {
            return Page.empty();//空地分页对象
        }
        //当前页显示数据list：select*from xxxwhere。。。limit？,？
        Pageable pageable=PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<Region> list=template.find(query, Region.class);
        return new PageImpl<Region>(list, pageable, total);//PageImpl代替计算
        //return DBHelper.query(template, Region.class, query, pageable);
    }

    @Override
    public void saveOrUpdate(Region region) {
//字符串id必须实现下面操作
        if (StringUtils.hasLength(region.getId())) {
            //update
            //特殊案例, 页面更新的数据, 刚好跟集合中字段个数一致
            //所以这种等价于全量更新
            this.update(region);
        } else {
            //save
            this.save(region);
        }
    }

    @Override
    public void changeHotValue(String id, int hot) {
       /* //1:查询
        Region region=this.get(id);
        //2:替换
        region.setIshot(hot);
        //3:更新
        this.update(region);*/
        /**
         * 部分字段更新
         * db.集合.updateMany({_id:id},{$set:{ishot:hot}})
         */
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update=new Update();
        update.set("ishot",hot);
        template.updateMulti(query,update,Region.class);
    }
    @Override
    public List<Region> queryHotRegion() {

        //1:状态是热门
        //2:通过序号排序
        return repository.findByIshotOrderBySequence(Region.STATE_HOT);
    }

}
